#工厂模式
from  info import create_app, db, models
from flask_script import Manager
from flask_migrate import Migrate,MigrateCommand
from flask import current_app
from info import response
# app = create_app("production")
app = create_app("development")
manager = Manager(app)

migrate = Migrate(app, db)
manager.add_command("db",MigrateCommand)

#创建管理员.自定义终端命令方法
#python manage.py createsuperuser -n 123 -p 456
@manager.option('-n',dest = 'name')
@manager.option('-p',dest = 'password')
def createsuperuser(name,password):
    #验证name是否存在
    try:
        if models.User.query.filter_by(mobile = name).count() > 0:
            print('该用户已存在')
            return
    except Exception as  e:
        current_app.logger.crror(e)
        print('链接数据库失败')
        return


    user = models.User()
    user.mobile = name
    user.nick_name = name
    user.password = password
    user.is_admin = True
    db.session.add(user)
    try:
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        print("连接数据库失败")
        return
    print('创建管理员成功')
if __name__ == '__main__':
    manager.run()

